package net.cl.support.ui;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeNode;

public class TreeNodeRenderer extends DefaultTreeCellRenderer {
	private static final long serialVersionUID = 8532405600839140757L;

	// 數據庫圖標,頂層節點用
	private Icon databaseIcon = new ImageIcon(getClass().getResource(
			"images/computer.png"));

	// 表圖標,第三層節點用
	private Icon tableIcon = new ImageIcon(getClass().getResource(
			"images/table.png"));

	// 表空間關閉狀態圖標,關閉狀態的第二層節點用
	private Icon tableSpaceCloseIcon = new ImageIcon(getClass().getResource(
			"images/database.png"));

	// 表空間關閉狀態圖標,打開狀態的第二層節點用
	private Icon tableSpaceOpenIcon = new ImageIcon(getClass().getResource(
			"images/database_go.png"));

	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean sel, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {
		super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
				row, hasFocus);
		// 取得節點
		DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

		// 取得路徑
		TreeNode[] paths = node.getPath();

		// 按路径层次赋予不同的图标
		if (paths.length == 3) {
			setIcon(tableIcon);
		} else if (paths.length == 2) {
			// 按展開情況再賦予不同的圖標
			if (expanded) {
				setIcon(tableSpaceOpenIcon);
			} else {
				setIcon(tableSpaceCloseIcon);
			}
		} else if (paths.length == 1) {
			setIcon(databaseIcon);
		}

		return this;
	}
}
